/**
 * HOMER Core - an open and flexible OSGi-based software platform for SmartHome and AAL applications
 *
 * Copyright (C) 2011-2013 AIT Austrian Institute of Technology GmbH - <http://www.ait.ac.at>
 * Contact: Thomas Fuxreiter <thomas.fuxreiter@ait.ac.at>
 *
 * This file is part of HOMER Core.
 *
 * HOMER Core is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * HOMER Core is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package at.aal2aac.eventbusclient;

import java.lang.reflect.Type;


import at.aal2aac.eventbusclient.Utils;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
 * Implementation of the {@link JsonSerializer} and {@link JsonDeserializer} for {@link EventbusMessage} objects.
 * This adapter is connected to the {link GsonBuilder} instance used for (de)serialization.
 * 
 * @author kropfj
 *
 */
public final class EventbusMessageGsonAdapter implements JsonSerializer<EventbusMessage<?>>, JsonDeserializer<EventbusMessage<?>> {


	@Override
	public EventbusMessage<?> deserialize(final JsonElement json, final Type type, final JsonDeserializationContext context) {
		// Cast to a JsonObject
		JsonObject jObject = (JsonObject) json;

		// Get the fields for an EventbusMessage
		EventbusMessage msg = new EventbusMessage();
		msg.setAddress(jObject.get("address").getAsString());
		msg.setMode(jObject.get("mode").getAsString());
		msg.setReplyAddress(jObject.get("replyAddress") != null ? jObject.get("replyAddress").getAsString() : null);
		msg.setSenderId(jObject.get("senderId") != null ? jObject.get("senderId").getAsString() : null);

		String bodyType = jObject.get("bodyType").getAsString();

		Class<?> clazz = null;
		try {
			clazz = bodyType != null ? Utils.getClassByName(bodyType) : null;
			if (clazz != null) {
				msg.setBody(context.deserialize(jObject.get("body").getAsJsonObject(), clazz));
			} else {
			}

		} catch (ClassNotFoundException ex) {
		}

		return msg;
	}

	@Override
	public JsonElement serialize(final EventbusMessage<?> msg, final Type type, final JsonSerializationContext context) {
		JsonObject result = new JsonObject();
		result.add("address", msg.getAddress() != null ? new JsonPrimitive(msg.getAddress()) : JsonNull.INSTANCE);
		result.add("mode", msg.getMode() != null ? new JsonPrimitive(msg.getMode()) : JsonNull.INSTANCE);
		result.add("replyAddress", msg.getReplyAddress() != null ? new JsonPrimitive(msg.getReplyAddress()) : JsonNull.INSTANCE);
		result.add("bodyType", msg.getBodyType() != null ? new JsonPrimitive(msg.getBodyType().getName()) : JsonNull.INSTANCE);
		result.add("senderId", msg.getSenderId() != null ? new JsonPrimitive(msg.getSenderId()) : JsonNull.INSTANCE);

		result.add("body", context.serialize(msg.getBody()));

		return result;
	}
}
